#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
except:
    pass
import jarryLibs.mayakit.MayaUtils as mu
reload(mu)

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckUnloadReference(CheckBase):
    def __init__(self):
        super(CheckUnloadReference, self).__init__()
        pass

    def get_unload_ref(self):
        unload_reference_list = [r for r in pm.listReferences() if not r.isLoaded()]
        return unload_reference_list

    def check(self):
        unload_reference_list = self.get_unload_ref()

        if unload_reference_list:
            result_log = u'Error:查询到未加载的参考:%s' % [str(r.path) for r in unload_reference_list]
        else:
            result_log = u'Succeed:没有问题\n'
        return result_log
    
    def fix(self):
        unload_reference_list = self.get_unload_ref()

        if unload_reference_list:
                result_log = 'Succeed:'
                for r in unload_reference_list:
                    path = r.path
                    try:
                        r.remove()
                        result_log += u'<br>%s:移除成功;' % path
                    except:
                        result_log += u'<br>%s:移除失败;' % path
                return result_log
        else:
            return u'Succeed:没有问题\n'
